package Dao;

import Bean.Department;
import Bean.OrderGoods;
import Utils.JDBCUtils;

import javax.imageio.stream.IIOByteBuffer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DepartDao {

    public static List<Department> getDepartmentList(){
        List<Department> list = new ArrayList<>();
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            con = JDBCUtils.getConnection();
            String sql = "select * from tb_department";
            //执行sql
            pst = con.prepareStatement(sql);
            //接收结果
            rs = pst.executeQuery();
            while (rs.next()){
                Department der = new Department();
                der.setId(rs.getInt(1));
                der.setName(rs.getString(2));
                der.setCharging(rs.getString(3));
                der.setDescribe(rs.getString(4));
                list.add(der);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return list;
    }

    public static List<Department> query(String item, String text){
        List<Department> list = new ArrayList<>();
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String i = "";
        if("编号".equals(item)){
            i = "select * from tb_department where id = ?";
        }else if("部门名称".equals(item)){
            i = "select * from tb_department where name = ?";
        }else if("负责人".equals(item)){
            i="select * from tb_department where charing = ?";
        }else{
            i="select * from tb_department where  des =?";

        }
        try {
            con = JDBCUtils.getConnection();
            System.out.println(i);
            //执行sql
            pst = con.prepareStatement(i);
            //接收结果
            pst.setString(1,text);
            rs = pst.executeQuery();
            while (rs.next()){
                Department der = new Department();
                der.setId(rs.getInt(1));
                der.setName(rs.getString(2));
                der.setCharging(rs.getString(3));
                der.setDescribe(rs.getString(4));
                list.add(der);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }

        return list;
    }

    public static List<Integer> findId(){
        List<Integer> list =new ArrayList<>();
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            con = JDBCUtils.getConnection();
            String sql = "select id from tb_department";
            //执行sql
            pst = con.prepareStatement(sql);
            //接收结果
            rs = pst.executeQuery();
            while (rs.next()){
                int id = rs.getInt(1);
                list.add(id);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return list;
    }

    public static boolean addInfo(Department dep){
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            con = JDBCUtils.getConnection();
            String sql = "insert into  tb_department values(?,?,?,?)";
            //执行sql
            pst = con.prepareStatement(sql);
            pst.setInt(1,dep.getId());
            pst.setString(2,dep.getName());
            pst.setString(3,dep.getCharging());
            pst.setString(4,dep.getDescribe());
            //接收结果
            int i = pst.executeUpdate();
            if(i>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return false;
    }

    public static boolean modInfo(Department dep){
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            con = JDBCUtils.getConnection();
            String sql = "update  tb_department  set name = ?,charing = ?,des = ? where id = ?";
            //执行sql
            pst = con.prepareStatement(sql);
            pst.setString(1,dep.getName());
            pst.setString(2,dep.getCharging());
            pst.setString(3,dep.getDescribe());
            pst.setInt(4,dep.getId());
            //接收结果
            int i = pst.executeUpdate();
            if(i>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return false;
    }

    public static boolean delInfo(int id){
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        try {
            con = JDBCUtils.getConnection();
            String sql = "delete from tb_department where id = ?";
            //执行sql
            pst = con.prepareStatement(sql);
            //设置参数
            pst.setInt(1,id);
            //接收结果
            int i = pst.executeUpdate();
            if(i>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return false;
    }
}
